package com.luo.shop.controller;

import com.luo.shop.pojo.Comment;
import com.luo.shop.pojo.User;
import com.luo.shop.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.swing.tree.VariableHeightLayoutCache;
import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;


    @PostMapping("save")
    @ResponseBody
    public String add(Comment comment, HttpSession session, Model model){
        User user =  (User)session.getAttribute("portalUser");
        comment.setUid(user.getId());
        commentService.save(comment);
        return "ok";
    }

    @GetMapping("delComment")
    @ResponseBody
    public String del(Long id,Long gid,Model model){
        Integer del = commentService.del(id);
        if (del>0){
            return "ok";
        }
        return "error";

    }

    @GetMapping("myprodByGid")
    @ResponseBody
    public Comment myprodByGid(Long  gid,HttpSession session){
        User user =  (User)session.getAttribute("portalUser");
        List<Comment> comments = commentService.queryByGid(gid);
        if (comments!=null){
            for (Comment comment : comments) {
                if (comment.getUid()==user.getId() && comment.getImages()!=null){
                    return comment;
                }
            }
        }
        return null;
    }

}
